package com.zmx.piglearn.receiver

import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.WindowManager
import com.zmx.piglearn.activity.ActivityCollector
import com.zmx.piglearn.activity.LoginActivity

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("###", "started")
        val builder = AlertDialog.Builder(context)
        builder.setTitle("警告：")
        builder.setMessage("您的账号在别处登录，请重新登陆~")
        builder.setCancelable(false)
        builder.setPositiveButton("确定") { dialog, which ->
            ActivityCollector.finishAll()
            val it = Intent(context, LoginActivity::class.java)
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 必要
            context?.startActivity(it)
        }
        val dialog = builder.create()
        // 窗口类型
        val type = if (Build.VERSION.SDK_INT > 25) {
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        }
        dialog.window?.setType(type) // 必要
        dialog.show()
    }
}